import type { FieldValidator, FormItemProps, IFormGridProps, ISchema, SchemaReaction } from '@minmetals-ui/pro-components';
import type { CSSProperties } from 'vue';
// import type { TabsProps } from 'element-plus-cisdi'

export interface RecordType {
  [key: string]: any
}

export interface QueryConditionSchema {
  /**
   * @description 给表单的字段名
   * @example username password
   */
  field: string
  /**
   * @description 字段展示的名称
   * @example 用户名 密码
   */
  fieldTitle?: string
  /**
   * @description 字段渲染哪个组件
   */
  fieldComponent: string
  /**
   * @description 字段渲染组件所需的props
   */
  fieldProps?: RecordType
  /**
   * @description FormItem的Props
   * @link https://element-plus.formilyjs.org/guide/form-item.html#api
   */
  fieldItemProps?: FormItemProps & { style?: CSSProperties }
  /**
   * @description 字段校验规则
   * @link https://core.formilyjs.org/api/models/field#fieldvalidator
   */
  fieldValidator?: FieldValidator
  /**
   * @description 字段联动协议
   * @link https://vue.formilyjs.org/api/shared/schema.html#schemareactions
   */
  fieldReactions?: Partial<SchemaReaction>

  defaultValue?: string | number | boolean | RecordType
}

export interface IGridProps {
  minColumns?: number | number[]
  maxColumns?: number
  labelCol: number
  wrapperCol: number
}

export interface QueryConditionProps {

  /**
   * 查询条件
   */
  queryCondition: Array<QueryConditionSchema>

  /**
   * 是否禁用
   */
  disabled?: boolean
  /**
   * 是否允许点击蒙层关闭弹窗
   */
  closeOnClickModal?: boolean

  /**
   * @description 对话框是否可见
   */
  dialogVisible?: boolean
  /**
   *  初始值
   */
  assignModelValue?: RecordType
  /**
   * 标题
   */
  title?: string

  dialogWidth?: string | number
  /**
   * grid 布局
   * @link https://element-plus.formilyjs.org/guide/form-grid.html#api
   */
  grid?: IFormGridProps['grid'] | IGridProps
  tableData?: Array<RecordType>
  columnDialog?: Array<RecordType>
  queryAssignUser?: (params: AssignUserParam) => Promise<UserResponse>
  pageData: { current: number, size: number, total: number }
}

export interface AssignPropEmits {
  (event: 'closeDialog'): void
  (event: 'submitDialog', data: Array<RecordType>): void
  (event: 'deleteUser', data: number[]): void
  (event: 'openAddAssignUser'): void
}

export interface UserTreeProps {
  title?: string
  dialogVisible?: boolean
  tableData?: Array<RecordType>
  selectedValue?: DataTypes[]
  orgCode: string | number
}

export interface UserTreePropEmits {
  (event: 'closeDialog'): void
  (event: 'submitDialog', data: Array<RecordType>): void
}

export type DefaultRow = Record<PropertyKey, any>;

export interface RoleResponse {
  code?: string
  message?: string
  data?: RoleRecord[]
  total?: string | number
  current?: string | number
  size?: string | number
}

export interface RoleRecord {
  roleId?: number
  roleCode?: string
  roleName?: string
  subVisible?: number
  roleOrgCode?: number
  roleOrgName?: string
  description?: string
  inheritedOrgCode?: string
  inheritedOrgName?: string
  inheritedRoleId?: number
  inheritedRoleName?: string
  remark?: string
  createTime?: string
  createBy?: number
  createName?: string
  updateTime?: string
  updateBy?: number
  updateName?: string
  status?: number | string
}

export interface RolePageData {
  current?: string
  size?: string
  total?: string
  records?: RoleRecord[]
}

export interface CommonResponse {
  code?: number | string
  message?: string
}

export interface QueryAssignParams {
  roleId?: number
  orgCode?: string
  current?: number
  size?: number
  search?: string | number
}

export interface UserResponse {
  code?: string
  message?: string
  data?: UserRecord[]
  total?: number | string
}

export interface UserRecord {
  id?: number
  userId?: number
  userCode?: string
  userName?: string
  orgCode?: string
  orgName?: string
  createTime?: string
  createBy?: number
  createName?: string
  avatarUrl?: string
}

export interface RoleUserAssignment {
  roleId?: number
  orgCode?: string
  userId?: number
  userName?: string
  users?: UserAssignment[]
}

export interface UserAssignment {
  userId?: number
  userCode?: string
  userName?: string
  orgCode?: string
}

export interface ApiResponse {
  code?: string
  message?: string
  data?: {
    [key: string]: MapKeyItem[]
  }
  requestId?: string
  error?: ErrorDetail
}

interface MapKeyItem {
  id?: number | string
  code?: string
  label?: string
  orgFullName?: string
  type?: string
  parentCode?: string
  isLeaf?: number
  isExtend?: number
  canCheck?: number
  createBy?: string
  updateBy?: string
  createTime?: string
  updateTime?: string
  fullCodePath?: string
  bindOrgCode?: string
  children?: MapKeyItem[]
}

interface ErrorDetail {
  code?: string
  traceId?: string
  path?: string
  timestamp?: number
  details?: string
}

export interface InheritResponse {
  code?: string
  message?: string
  data?: InheritItem[]
}

export interface InheritItem {
  roleId?: number
  roleCode?: string
  roleName?: string
  roleOrgCode?: number
  roleOrgName?: string
  children?: InheritItem[]
}

export interface InheritQuery {
  orgCode?: string
  roleId?: number
}

export interface UserData {
  userId?: number
  userName?: string
  data?: RoleData[]
}

export interface RoleData {
  roleId?: number
  status?: number | string
}

export interface SelectItem {
  value: string | number
  label: string | number
  data: RecordType
}

export interface TreeTable {
  loadData: any
}

export interface PageCommonSchema extends PartialSchema {
  /**
   * @description 给表单的字段名
   * @example username password
   */
  field: string
  /**
   * @description 字段展示的名称
   * @example 用户名 密码
   */
  fieldTitle?: string
}

export interface PartialSchema {
  /**
   * @description 字段渲染哪个组件
   */
  fieldComponent: string
  /**
   * @description 字段渲染组件所需的props
   */
  fieldProps?: RecordType
  /**
   * @description FormItem的Props
   * @link https://element-plus.formilyjs.org/guide/form-item.html#api
   */
  fieldItemProps?: FormItemProps & { style?: CSSProperties }
  /**
   * @description 字段校验规则
   * @link https://core.formilyjs.org/api/models/field#fieldvalidator
   */
  fieldValidator?: FieldValidator
  /**
   * @description 字段联动协议
   * @link https://vue.formilyjs.org/api/shared/schema.html#schemareactions
   */
  fieldReactions?: Partial<SchemaReaction>
  /**
   * @description 分组名称
   */
  groupName?: string
  /**
   * 分组id
   */
  groupId?: string
  /**
   * 编辑时是否可编辑
   */
  isEditingDisabled?: boolean
  /**
   * 新增时是否可编辑
   */
  isCreatingDisabled?: boolean
  /**
   * 默认值
   */
  defaultValue?: any
}

export interface ColumnDialogProps {
  /**
   * @description 对话框是否可见
   */
  visible?: boolean
  /**
   *  初始值
   */
  modelValue: {
    roleCode: string | number
    roleName: string
    description: string
    inheritedRoleId: string | number
    subVisible: number | string
    status: number | string
    remark: string
  }
  /**
   * 查询条件
   */
  queryCondition: Array<PageCommonSchema>
  /**
   * 标题
   */
  title?: string
  /**
   * 组件布局
   */
  grid?: IGridProps
  /**
   * 对话框大小
   */
  dialogSize?: 'large' | 'small' | 'medium'
  /**
   * 所有字段的 FormItemProps
   */
  allFieldItemProps?: FormItemProps & { style?: CSSProperties }
  dialogDisabled?: boolean
  dialogMode?: 'edit' | 'add'
  inheritList: SelectItem[]
}

export interface RoleModelValue {
  roleCode: string | number
  roleName: string
  description: string
  inheritedRoleId: string | number
  subVisible: number | string
  status: number | string
  remark: string
  [key: string]: any
}

export interface ColumnDialogEmits {
  (event: 'change', value: RecordType): void
  (event: 'update:visible', value?: boolean): void
}

export function transformQueryCondition(data?: PageCommonSchema[], allFieldItemProps?: FormItemProps & { style?: CSSProperties }) {
  const result: Record<string, ISchema> = {};
  data?.forEach((item) => {
    result[item.field] = {
      'type': 'string',
      'title': item.fieldTitle,
      'x-decorator': 'FormItem',
      'x-decorator-props': item.fieldItemProps || allFieldItemProps || { labelCol: 8, wrapperCol: 16 },
      'x-component': item.fieldComponent,
      'x-component-props': item.fieldProps,
      'x-validator': item.fieldValidator,
      'x-reactions': item.fieldReactions,
      'default': item.defaultValue,
    };
  });
  return result;
}

export interface RoleListParam {
  current?: number
  size?: number
  orgCode: string
  scope?: string | number
  [key: string]: any
  search?: string
}

export interface AssignUserParam {
  current?: number
  size?: number
  orgCode: string
  roleId: string | number
  search?: string
}

export interface InheritParam {
  orgCode: string
  roleId?: string | number
  [key: string]: any
}

// 角色信息接口 - 所有字段都是可选的
export interface RoleInfoParam {
  roleId?: number
  roleCode?: string
  roleName?: string
  roleOrgCode?: number
  roleOrgName?: string
  subVisible?: number
  inheritedRoleOrgCode?: number | string // 根据实际情况可能是数字或字符串
  inheritedRoleOrgName?: string
  inheritedRoleId?: number
  inheritedRoleName?: string
  description?: string
  remark?: string
  createTime?: string
  createBy?: string | number // 可能是字符串或数字
  createName?: string
  updateTime?: string
  updateBy?: string | number // 可能是字符串或数字
  updateName?: string
  status: string | number | undefined
}

export interface OrgTree {
  orgCode?: number | string
  isTree?: number | string
  loadType?: number | string
  extendcondition?: string
}

export interface DataTypes {
  id?: string
  code: string
  label?: string
  orgFullName?: string
  parentCode?: string
  isLeaf?: string | boolean
  isExtend?: string
  type?: string
  bingOrgCode?: string
  createBy?: string
  updateBy?: string
  createTime?: string
  updateTime?: string
  isUser?: boolean
  users?: DataTypes[]
  [key: string]: any
  orgFullNamePath?: string
}

export interface DeleteResponse<T = any> {
  data: T
  code: number | string
  message: string
}
